package com.study.happystudy.tool;

import com.aliyun.alimt20181012.models.TranslateGeneralResponse;
import com.aliyun.tea.TeaException;
import org.springframework.stereotype.Component;

@Component
public class Translators {

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId     LTAI5tNhUt47h3f8qhrTCUwG
     * @param accessKeySecret lbBy6AADOouspUEm9RXk7Unlg8a2nc
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.alimt20181012.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId("LTAI5tHWnXJtqXwBb5RGs7ay")
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret("UxZPgZv4KjozmiHeDPZUoCvskZjrAu");
//                .setEndpoint("https://alimt.aliyuncs.com");
//                .setRegionId("cn-hangzhou");
        // Endpoint 请参考 https://api.aliyun.com/product/alimt
        config.endpoint = "mt.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.alimt20181012.Client(config);
    }

    public String translate(String SourceLanguage, String TargetLanguage, String SourceText) throws Exception {
//        java.util.List<String> args = java.util.Arrays.asList(args_);
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.alimt20181012.Client client = Translators.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        com.aliyun.alimt20181012.models.TranslateGeneralRequest translateGeneralRequest = new com.aliyun.alimt20181012.models.TranslateGeneralRequest()
                .setFormatType("text")//翻译文本的格式
                .setSourceLanguage(SourceLanguage)//原文
                .setTargetLanguage(TargetLanguage)//译文
                .setSourceText(SourceText)//需要翻译的内容
                .setScene("general");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            TranslateGeneralResponse response = client.translateGeneralWithOptions(translateGeneralRequest, runtime);
            System.out.println(response.body.data.translated);
            return response.body.data.translated;
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            return error.message;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            return error.message;
        }
    }
}
